1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 import sun.net.www.HeaderParser;
33 import sun.net.www.MessageHeader;
34 import sun.net.www.protocol.http.AuthenticationHeader;
35 import java.io.*;
36 import java.util.Iterator;
37
38 public class HeaderTests {
39
40 static MessageHeader createMessageHeader (String s) {
41 ByteArrayInputStream bis = new ByteArrayInputStream (s.getBytes());
42 MessageHeader h = new MessageHeader ();
43 try {
44 h.parseHeader (bis);
45 } catch (IOException e) {
46 throw new RuntimeException ("IOException parsing header");
47 }
48 return h;
49 }
50
51
52
53 static String s1 =
54 "Foo: bar\r\n"+
55 "Fub: abc\r\n"+
56 "Foo:\r\n"+
57 "Fub: \r\n"+
58 "Foo: param1=one param2=\"two\" param3 param4=\"value=4\"\r\n"+
59 "Fub: xparam1=one xparam2=\"two\" xparam3 xparam4=\"value=4\"\r\n";
60
61 static String s2 = "p1=1 p2=2 p3=3 p4=4 p5=5 p6=\"six\" p7=7 p8=8 p9=9 p10=10 p11=11 p12=12";
62
63 static String s3 = "p1=1, p2=2, p3=3, p4=4, p5=5, p6=\"six\", p7=7, p8=8, p9=9, p10=10, p11=11, p12=12";
64
65 static String s23_expect[][] = {{"p2","2"},{"p3","3"},{"p4","4"},{"p5","5"},{"p6","six"},
66 {"p7","7"},{"p8","8"},{"p9","9"},{"p10","10"},{"p11","11"},{"p12","12"}};
67
68 static String s23_expect1[][] = {{"p1","1"},{"p2","2"},{"p3","3"},{"p4","4"},{"p5","5"},{"p6","six"},
69 {"p7","7"},{"p8","8"},{"p9","9"},{"p10","10"},{"p11","11"}};
70
71 static String s23_expect2[][] = {{"p5","5"},{"p6","six"}};
72
73
74 static String s1_Foo[][][] = {{{"bar", null}
75 },
76 {{"param1","one"},{"param2","two"},
77 {"param3", null},{"param4","value=4"}
78 }
79 };
80
81
82 static String s1_Fub[][][] = {{{"abc", null}
83 },
84 {{"xparam1","one"},{"xparam2","two"},
85 {"xparam3", null},{"xparam4","value=4"}
86 }
87 };
88
89 public static void main (String args[]) {
90 MessageHeader head = createMessageHeader (s1);
91 Iterator iter = head.multiValueIterator ("Foo");
92 checkHeader (iter, s1_Foo);
93 iter = head.multiValueIterator ("Fub");
94 checkHeader (iter, s1_Fub);
95
96 HeaderParser hp = new HeaderParser (s2).subsequence (1,12);
97 check (hp, s23_expect);
98
99 hp = new HeaderParser (s3).subsequence (1,12);
100 check (hp, s23_expect);
101
102 hp = new HeaderParser (s3).subsequence (0,11);
103 check (hp, s23_expect1);
104
105 hp = new HeaderParser (s2).subsequence (4,6);
106 check (hp, s23_expect2);
107 }
108
109 static void checkHeader (Iterator iter, String[][][] expect) {
110 for (int i=0; iter.hasNext (); ) {
111 String s = (String) iter.next();
112 HeaderParser p = new HeaderParser (s);
113 boolean empty = check (p, expect[i]);
114 if (!empty) {
115 i++;
116 }
117 }
118 }
119
120 static boolean check (HeaderParser p, String[][]expect) {
121 Iterator keys = p.keys();
122 Iterator vals = p.values();
123 boolean empty = true;
124 for (int j=0; keys.hasNext(); j++) {
125 empty = false;
126 String key = (String)keys.next();
127 String ival = (String)vals.next();
128 String val = p.findValue (key);
129 if (val == null && ival == null)
130 continue;
131 if (!val.equals(ival)) {
132 throw new RuntimeException ("Error " + val + "/" + ival);
133 }
134 if (!expect[j][0].equals (key)) {
135 throw new RuntimeException ("Error "+key+"/" + expect[j][0]);
136 }
137 if (expect[j][1] != null && !expect[j][1].equals (val)) {
138 throw new RuntimeException ("Error "+val+"/" + expect[j][1]);
139 }
140 }
141 return empty;
142 }
143 }